package com.example.demo.controller;

import com.example.demo.dto.Response;
import com.example.demo.entity.Notice;
import com.example.demo.service.NoticeService;
import lombok.extern.log4j.Log4j2;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (Notice)表控制层
 *
 * @author makejava
 * @since 2022-01-12 14:48:15
 */
@Log4j2
@RestController
@RequestMapping("beeorder/notice")
public class NoticeController {
    /**
     * 服务对象
     */
    @Resource
    private NoticeService noticeService;

    /**
     * 分页查询
     *
     * @param notice      筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @GetMapping
    public Response<Page<Notice>> queryByPage(Notice notice, PageRequest pageRequest) {
//        return ResponseEntity.ok(this.noticeService.queryByPage(notice, pageRequest));
        Response response = new Response();
        try {
            return response.SUCCESS(this.noticeService.queryByPage(notice, pageRequest));
        } catch (Exception e) {
            log.info("查询异常,异常原因：{}",e.getLocalizedMessage());
            return response.FAIL(e.getLocalizedMessage());
        }
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Notice> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.noticeService.queryById(id));
    }


    /**
     * 查询最新的消息
     *
     * @return 单条数据
     */
    @GetMapping("queryByIsShow")
    public ResponseEntity<Notice> queryByIsShow() {
        return ResponseEntity.ok(this.noticeService.queryByIsShow(1));
    }
    /**
     * 新增数据
     *
     * @param notice 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Notice> add(Notice notice) {
        return ResponseEntity.ok(this.noticeService.insert(notice));
    }

    /**
     * 编辑数据
     *
     * @param notice 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Notice> edit(Notice notice) {
        return ResponseEntity.ok(this.noticeService.update(notice));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.noticeService.deleteById(id));
    }

}

